import json

from fastapi import APIRouter
from starlette.requests import Request
from xbase_util.db.bean.ConfigBean import ConfigBean

from base.bean.beans import ApiConfigBean, ApiCommonBean
from base.util.api_util import ApiUtil

from capture_api import configDao, get_current_config_
from global_ver import config_types

configRouter = APIRouter()


@configRouter.post('/get_config', description="获取当前配置信息")
async def api_get_config(bean: ApiCommonBean):
    config = get_current_config_()
    if not isinstance(config, ConfigBean):
        return config
    type_ = bean.type
    config_dict = {}
    if type_ == "session":
        config_dict = config.to_session_dict()
    if type_ == "pcap":
        config_dict = config.to_pcap_dict()
    if type_ == "label":
        config_dict = config.to_label_dict()
    if type_ == "mapping":
        config_dict = config.to_mapping()
    if type_ == "replace":
        config_dict = config.to_replace()
    return ApiUtil.wrap_correct(config_dict)


@configRouter.post('/get_functions', description="获取当前有哪些可以用的流程步骤")
def get_functions():
    return ApiUtil.wrap_correct(config_types)


@configRouter.post('/set_config', description="修改当前配置文件的配置信息")
def api_set_config(bean: ApiConfigBean):
    if bean.type_ == "session":
        configDao.set_config_session_by_id(id=bean.id,
                                           session_all_true=bean.session_all_true,
                                           session_alive=bean.session_alive,
                                           session_size=bean.session_size,
                                           session_expression=bean.session_expression,
                                           session_start_time=bean.session_start_time,
                                           session_end_time=bean.session_end_time,
                                           )
    if bean.type_ == "pcap":
        configDao.set_config_pcap_by_id(id=bean.id,
                                        pcap_process=bean.pcap_process,
                                        pcap_per_subsection=bean.pcap_per_subsection,
                                        pcap_thread_in_process=bean.pcap_thread_in_process,
                                        )
    if bean.type_ == "label":
        configDao.set_config_label_by_id(id=bean.id,
                                         label_all_true=bean.label_all_true,
                                         label_duration=bean.label_duration,
                                         label_is_output_unmatch=bean.label_is_output_unmatch,
                                         )
    if bean.type_ == "mapping":
        configDao.set_config_mapping_by_id(id=bean.id,
                                           mapping_le_path=bean.mapping_le_path
                                           )
    if bean.type_ == "replace":
        configDao.set_config_replace_by_id(id=bean.id,
                                           replace_source=bean.replace_source,
                                           replace_mapping=bean.replace_mapping,
                                           replace_destination=bean.replace_destination,
                                           replace_save_to=bean.replace_save_to
                                           )
    return ApiUtil.wrap_correct("保存完毕")


@configRouter.post('/run_single_step', description="执行单个、非流程化的步骤")
def run_single_step(request: Request, bean: ApiCommonBean):
    config = get_current_config_()
    if config.catalogue is None or config.catalogue == '':
        config.catalogue = ApiUtil.get_default_catalogue()
    request.app.state.threadManager.start_thread(ApiUtil.single_step, bean.type, config,
                                                 request.app.state.base_config,
                                                 request.app.state.redisUtil,
                                                 request.app.state.geoUtil,
                                                 request.app.state.esdb,
                                                 )
    return ApiUtil.wrap_correct("开始运行")


@configRouter.post('/add_config_file', description="添加配置文件")
def add_config_file(bean: ApiConfigBean):
    configDao.add(
        id=bean.id,
        description=bean.description,
        label_all_true=bean.label_all_true,
        label_is_output_unmatch=bean.label_is_output_unmatch,
        label_duration=bean.label_duration,
        mapping_le_path=bean.mapping_le_path,
        pcap_per_subsection=bean.pcap_per_subsection,
        pcap_process=bean.pcap_process,
        pcap_thread_in_process=bean.pcap_thread_in_process,
        replace_source=bean.replace_source,
        replace_destination=bean.replace_destination,
        replace_mapping=bean.replace_mapping,
        replace_save_to=bean.replace_save_to,
        session_end_time=bean.session_end_time,
        session_all_true=bean.session_all_true,
        session_start_time=bean.session_start_time,
        session_expression=bean.session_expression,
        session_alive=bean.session_alive,
        session_size=bean.session_size,
        splitNumber=bean.splitNumber,
        catalogue=bean.catalogue,
    )
    return ApiUtil.wrap_correct("ok")


@configRouter.post('/remove_config_file', description="删除配置文件")
def remove_config_file(bean: ApiCommonBean):
    configDao.remove_by_id(bean.id)
    return ApiUtil.wrap_correct("ok")


@configRouter.post('/get_run_status', description="获取运行状态")
def get_run_status(request: Request):
    status = request.app.state.redisUtil.get_status()
    if status is None:
        return ApiUtil.wrap_correct({})
    res = json.loads(status)
    thread_list = request.app.state.threadManager.get_thread_status()
    res['isRunning'] = True if thread_list else False
    res['taskList'] = thread_list
    return ApiUtil.wrap_correct(res)


@configRouter.post('/get_config_file_list', description="获取配置文件列表")
def get_config_file_list():
    return ApiUtil.wrap_correct(configDao.get_config_file_list())


@configRouter.post('/get_config_file_by_id', description="通过id获取配置文件")
def get_config_file(bean: ApiCommonBean):
    return ApiUtil.wrap_correct(configDao.get_config_by_id(bean.id))
